﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Controls.Primitives;
using System.Windows;




namespace SQLServerHealthCheck.Model
{
    public static class CollectionData
    {
        public static string[] GetAuthentication()
        {
            Dictionary<int, string> AuthenticationModes = new Dictionary<int, string>();
            AuthenticationModes.Add(0, "Windows Authentication");
            AuthenticationModes.Add(1, "SQL Server Authentication");

            return AuthenticationModes.Values.ToArray();
        }
    }
    #region PasswordBox Helper
    // this class Binds the password box, which is not ussually a dependency property.

    #endregion
    public class SQLServerInstance : INotifyPropertyChanged, IDataErrorInfo
    {
        // Property changed event handler         
        public event PropertyChangedEventHandler PropertyChanged;



        protected void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }






        // Property INstance Name with OnPropertyChanged Event 
        private string _instanceName;

        public String InstanceName
        {
            get { return _instanceName; }

            set
            {
                _instanceName = value;
                OnPropertyChanged("InstanceName");
            }
        }




        private string _login;
        public String Login
        {
            get { return _login; }


            set
            {

                _login = value;
                OnPropertyChanged("Login");

            }

        }

        private string _password;
        public String Password
        {
            get { return _password; }

            set
            {
                _password = value;
                OnPropertyChanged("Password");

            }
        }

        // Property Port Number with OnPropertyChanged Event 
        private string _port;
        public string Port
        {
            get { return _port; }

            set
            {
                _port = value;
                OnPropertyChanged("Port");
            }
        }
        private string _authentication;
        public string Authentication
        {

            get { return _authentication; }

            set
            {
                _authentication = value;
                OnPropertyChanged("Authentication");
            }


        }



        public string GetConnectionString(string DatabaseName)
        {
            string connectionString = "";

            string portNumber = "";

            if (!String.IsNullOrEmpty(_port) && _port.Trim() != "1433")
            {
                portNumber = "," + _port;
            }
            if (!_authentication.ToUpper().Contains("SQL"))
            {
                connectionString = string.Format("Server={0}{1};Database={2};Integrated Security=True;", this._instanceName, portNumber, DatabaseName);

            }
            else
            {

                connectionString = string.Format("Server={0}{1};Database={2};Integrated Security=False;User Id={3};Password={4}", this._instanceName, portNumber, DatabaseName, this._login, this._password);

            }
            return connectionString;
        }


        string IDataErrorInfo.Error
        {
            get
            {

                return null;

            }
        }

        string IDataErrorInfo.this[string columnName]
        {
            get
            {

                string result = null;



                // Port Validation Rules 
                if (columnName == "Port")
                {
                    int num = 0;

                    if ((!String.IsNullOrEmpty(_port) && !int.TryParse(_port, out num)))
                    {
                        result = "Not A Valid Port Number";

                    }


                }
                // Instance Name Validation Rules
                if (columnName == "InstanceName")
                {
                    if (String.IsNullOrEmpty(_instanceName))
                    {
                        result = "Please enter a SQL Server Instance Name";
                    }
                }

                return result;

            }
        }


    }
}
